// timer_test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "..\include\timer_module.h"
#include <stdio.h>
#include <iostream>
#include <time.h>
#include <boost\bind.hpp>
#include <Windows.h>
#include <string>
#include "..\include\common.h"

using namespace std;
using namespace utility::timer_util;
using namespace utility::system;

bool stop = false;

timer_module * timerModule;

void event1()
{
    static int i = 0;
    cout<<"event1 "<<time(NULL)<<endl;
    i++;
    if (i == 10)
    {
        timerModule->get_timer("timer2")->start();
        timerModule->get_timer("timer1")->stop();
        i = 0;
    };
}

class A
{
public:
    A() : i(0) {}

    void event2()
    {
        cout<< "A::event2 " << time(NULL) << " " << i++ <<endl;
        if (i == 5)
        {
            timerModule->get_timer("timer2")->stop();
            timerModule->get_timer("timer1")->start();
            i = 0;
        }
    }
protected:
    int i;
};



int _tmain(int argc, _TCHAR* argv[])
{
    timerModule =get_timer_module();
    if (timerModule)
    {
        timer * Timer1 = timerModule->create_timer("timer1");
        timer * Timer2 = timerModule->create_timer("timer2");
        Timer1->set_interval(1000);
        Timer1->set_event(event1);
        cout<<"Please input the start time for timer1:";
        string start_time;
        cin >> start_time;
        Timer1->set_start_time(str2time_t(start_time.c_str()));
        Timer1->start();

        Timer2->set_interval(2000);
        A a;
        Timer2->set_event(boost::bind(&A::event2, &a));

        while (!stop)
        {
            if (timerModule->run() == 0)
                Sleep(1);
        }
        Timer1->release();
        Timer2->release();
        timerModule->release();
    }
	return 0;
}

